package com.sgxy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sgxy.entity.EmployeeInf;
import com.sgxy.entity.JobInf;
import com.sgxy.service.EmployeeInfService;
import com.sgxy.service.JobInfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/job")
@Controller
public class JobInfController {

    private final JobInfService jobInfService;
    @Autowired
    public JobInfController(JobInfService jobInfService) {
        this.jobInfService = jobInfService;
    }
    @RequestMapping("/selectjob")
    public ModelAndView selectJobInfAction(JobInf jobInf,
                                           @RequestParam(value = "page", defaultValue = "1")Integer page){
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(page,10);
        List<JobInf> jobInfs = jobInfService.queryAllFJobInfs(jobInf);
        PageInfo<JobInf> jobInfPageInfo = new PageInfo<>(jobInfs);
        modelAndView.addObject("jobInfPageInfo",jobInfPageInfo);
        modelAndView.addObject("jobInf",jobInf);
        modelAndView.setViewName("/job/job.jsp");
        return modelAndView;
    }
    @RequestMapping("/toAddjob")
    public ModelAndView toAddjobAction(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/job/showAddJob.jsp");
        return modelAndView;
    }

    @RequestMapping("/addjob")
    public ModelAndView addJobInfAction(JobInf jobInf){
        ModelAndView modelAndView  = new ModelAndView();
        int i = jobInfService.addJobInf(jobInf);
        modelAndView.setViewName("redirect:/job/toAddjob");
        return modelAndView;
    }

    @RequestMapping("/toUpdatejob")
    public ModelAndView toUpdatejobAction(Integer id){
        ModelAndView modelAndView = new ModelAndView();
        JobInf jobInf = jobInfService.queryJobInf(id);
        modelAndView.addObject("jobInf",jobInf);
        modelAndView.setViewName("/job/showUpdateJob.jsp");
        return modelAndView;
    }
    @RequestMapping("/updatejob")
    public ModelAndView updateJobInfAction(JobInf jobInf){
        ModelAndView modelAndView = new ModelAndView();
        int i = jobInfService.updateJobInf(jobInf);
        modelAndView.setViewName("redirect:/job/selectjob");
        return modelAndView;
    }
    //返回值可能会有多个
    @RequestMapping("/deletejob")
    public ModelAndView deleteJobInf(String ids){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(ids);
        String[] split = ids.split(",");
        List<String> list = new ArrayList<String>();
        for(int i=0;i<split.length;i++){
            list.add(split[i]);
        }
        System.out.println(ids);
        int i = jobInfService.deleteJobInf(list);
        modelAndView.setViewName("redirect:/job/selectjob");
        return modelAndView;
    }
}
